//
//  AddBirthdayViewController.m
//  CangLongWnnianli
//
//  Created by 暗流澎涌 on 2025/1/27.
//

#import "AddBirthdayViewController.h"

@interface AddBirthdayViewController () <UITextViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UITextField *nameTextField;
@property (nonatomic, strong) UIPickerView *relationshipPicker;
@property (nonatomic, strong) UIPickerView *lunarMonthPicker;
@property (nonatomic, strong) UIPickerView *lunarDayPicker;
@property (nonatomic, strong) UITextField *yearTextField;
@property (nonatomic, strong) UITextView *noteTextView;
@property (nonatomic, strong) UISwitch *notifyOnDaySwitch;
@property (nonatomic, strong) UISwitch *notifyOneDayBeforeSwitch;
@property (nonatomic, strong) UISwitch *notifyOneWeekBeforeSwitch;

@property (nonatomic, strong) NSArray *relationshipOptions;
@property (nonatomic, strong) NSArray *lunarMonthOptions;
@property (nonatomic, strong) NSArray *lunarDayOptions;

@property (nonatomic, strong) NSString *selectedRelationship;
@property (nonatomic, assign) NSInteger selectedLunarMonth;
@property (nonatomic, assign) NSInteger selectedLunarDay;

@end

@implementation AddBirthdayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupData];
    [self setupUI];
    [self setupConstraints];
    [self setupKeyboardHandling];
}

- (void)setupData {
    self.relationshipOptions = @[@"朋友", @"家人", @"同事", @"其他"];
    self.lunarMonthOptions = @[@"正月", @"二月", @"三月", @"四月", @"五月", @"六月",
                              @"七月", @"八月", @"九月", @"十月", @"冬月", @"腊月"];
    
    NSMutableArray *days = [NSMutableArray array];
    for (int i = 1; i <= 30; i++) {
        if (i == 1) [days addObject:@"初一"];
        else if (i == 2) [days addObject:@"初二"];
        else if (i == 3) [days addObject:@"初三"];
        else if (i == 4) [days addObject:@"初四"];
        else if (i == 5) [days addObject:@"初五"];
        else if (i == 6) [days addObject:@"初六"];
        else if (i == 7) [days addObject:@"初七"];
        else if (i == 8) [days addObject:@"初八"];
        else if (i == 9) [days addObject:@"初九"];
        else if (i == 10) [days addObject:@"初十"];
        else if (i == 11) [days addObject:@"十一"];
        else if (i == 12) [days addObject:@"十二"];
        else if (i == 13) [days addObject:@"十三"];
        else if (i == 14) [days addObject:@"十四"];
        else if (i == 15) [days addObject:@"十五"];
        else if (i == 16) [days addObject:@"十六"];
        else if (i == 17) [days addObject:@"十七"];
        else if (i == 18) [days addObject:@"十八"];
        else if (i == 19) [days addObject:@"十九"];
        else if (i == 20) [days addObject:@"二十"];
        else if (i == 21) [days addObject:@"廿一"];
        else if (i == 22) [days addObject:@"廿二"];
        else if (i == 23) [days addObject:@"廿三"];
        else if (i == 24) [days addObject:@"廿四"];
        else if (i == 25) [days addObject:@"廿五"];
        else if (i == 26) [days addObject:@"廿六"];
        else if (i == 27) [days addObject:@"廿七"];
        else if (i == 28) [days addObject:@"廿八"];
        else if (i == 29) [days addObject:@"廿九"];
        else if (i == 30) [days addObject:@"三十"];
    }
    self.lunarDayOptions = days;
    
    self.selectedRelationship = self.relationshipOptions[0];
    self.selectedLunarMonth = 1;
    self.selectedLunarDay = 1;
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.0];
    self.title = @"添加生日提醒";
    
    // 导航栏按钮
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" 
                                                                      style:UIBarButtonItemStylePlain 
                                                                     target:self 
                                                                     action:@selector(cancelButtonTapped)];
    self.navigationItem.leftBarButtonItem = cancelButton;
    
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:@"保存" 
                                                                    style:UIBarButtonItemStyleDone 
                                                                   target:self 
                                                                   action:@selector(saveButtonTapped)];
    self.navigationItem.rightBarButtonItem = saveButton;
    
    // 创建滚动视图
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    self.contentView = [[UIView alloc] init];
    [self.scrollView addSubview:self.contentView];
    
    // 创建各个section
    [self createNameSection];
    [self createRelationshipSection];
    [self createLunarBirthdaySection];
    [self createNotificationSection];
    [self createNoteSection];
}

- (void)createNameSection {
    UIView *sectionView = [self createSectionViewWithTitle:@"姓名"];
    
    self.nameTextField = [[UITextField alloc] init];
    self.nameTextField.placeholder = @"请输入姓名";
    self.nameTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.nameTextField.font = [UIFont systemFontOfSize:16];
    self.nameTextField.backgroundColor = [UIColor whiteColor];
    [sectionView addSubview:self.nameTextField];
    
    self.nameTextField.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [self.nameTextField.topAnchor constraintEqualToAnchor:sectionView.topAnchor constant:50],
        [self.nameTextField.leadingAnchor constraintEqualToAnchor:sectionView.leadingAnchor constant:20],
        [self.nameTextField.trailingAnchor constraintEqualToAnchor:sectionView.trailingAnchor constant:-20],
        [self.nameTextField.heightAnchor constraintEqualToConstant:44],
        [self.nameTextField.bottomAnchor constraintEqualToAnchor:sectionView.bottomAnchor constant:-20]
    ]];
}

- (void)createRelationshipSection {
    UIView *sectionView = [self createSectionViewWithTitle:@"关系"];
    
    self.relationshipPicker = [[UIPickerView alloc] init];
    self.relationshipPicker.delegate = self;
    self.relationshipPicker.dataSource = self;
    self.relationshipPicker.backgroundColor = [UIColor whiteColor];
    self.relationshipPicker.layer.cornerRadius = 8;
    [sectionView addSubview:self.relationshipPicker];
    
    self.relationshipPicker.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [self.relationshipPicker.topAnchor constraintEqualToAnchor:sectionView.topAnchor constant:50],
        [self.relationshipPicker.leadingAnchor constraintEqualToAnchor:sectionView.leadingAnchor constant:20],
        [self.relationshipPicker.trailingAnchor constraintEqualToAnchor:sectionView.trailingAnchor constant:-20],
        [self.relationshipPicker.heightAnchor constraintEqualToConstant:120],
        [self.relationshipPicker.bottomAnchor constraintEqualToAnchor:sectionView.bottomAnchor constant:-20]
    ]];
}

- (void)createLunarBirthdaySection {
    UIView *sectionView = [self createSectionViewWithTitle:@"农历生日"];
    
    // 月份选择区域
    UIView *monthContainer = [[UIView alloc] init];
    monthContainer.backgroundColor = [UIColor whiteColor];
    monthContainer.layer.cornerRadius = 8;
    [sectionView addSubview:monthContainer];
    
    UILabel *monthLabel = [[UILabel alloc] init];
    monthLabel.text = @"月份";
    monthLabel.font = [UIFont systemFontOfSize:14];
    monthLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
    [monthContainer addSubview:monthLabel];
    
    self.lunarMonthPicker = [[UIPickerView alloc] init];
    self.lunarMonthPicker.delegate = self;
    self.lunarMonthPicker.dataSource = self;
    self.lunarMonthPicker.backgroundColor = [UIColor clearColor];
    [monthContainer addSubview:self.lunarMonthPicker];
    
    // 日期选择区域
    UIView *dayContainer = [[UIView alloc] init];
    dayContainer.backgroundColor = [UIColor whiteColor];
    dayContainer.layer.cornerRadius = 8;
    [sectionView addSubview:dayContainer];
    
    UILabel *dayLabel = [[UILabel alloc] init];
    dayLabel.text = @"日期";
    dayLabel.font = [UIFont systemFontOfSize:14];
    dayLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
    [dayContainer addSubview:dayLabel];
    
    self.lunarDayPicker = [[UIPickerView alloc] init];
    self.lunarDayPicker.delegate = self;
    self.lunarDayPicker.dataSource = self;
    self.lunarDayPicker.backgroundColor = [UIColor clearColor];
    [dayContainer addSubview:self.lunarDayPicker];
    
    // 出生年输入
    UILabel *yearLabel = [[UILabel alloc] init];
    yearLabel.text = @"出生年";
    yearLabel.font = [UIFont systemFontOfSize:14];
    yearLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
    [sectionView addSubview:yearLabel];
    
    self.yearTextField = [[UITextField alloc] init];
    self.yearTextField.placeholder = @"请输入出生年份";
    self.yearTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.yearTextField.font = [UIFont systemFontOfSize:16];
    self.yearTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.yearTextField.backgroundColor = [UIColor whiteColor];
    [sectionView addSubview:self.yearTextField];
    
    // 设置约束
    monthContainer.translatesAutoresizingMaskIntoConstraints = NO;
    monthLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.lunarMonthPicker.translatesAutoresizingMaskIntoConstraints = NO;
    dayContainer.translatesAutoresizingMaskIntoConstraints = NO;
    dayLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.lunarDayPicker.translatesAutoresizingMaskIntoConstraints = NO;
    yearLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.yearTextField.translatesAutoresizingMaskIntoConstraints = NO;
    
    [NSLayoutConstraint activateConstraints:@[
        // 月份容器
        [monthContainer.topAnchor constraintEqualToAnchor:sectionView.topAnchor constant:50],
        [monthContainer.leadingAnchor constraintEqualToAnchor:sectionView.leadingAnchor constant:20],
        [monthContainer.widthAnchor constraintEqualToAnchor:sectionView.widthAnchor multiplier:0.45],
        [monthContainer.heightAnchor constraintEqualToConstant:140],
        
        [monthLabel.topAnchor constraintEqualToAnchor:monthContainer.topAnchor constant:10],
        [monthLabel.leadingAnchor constraintEqualToAnchor:monthContainer.leadingAnchor constant:15],
        
        [self.lunarMonthPicker.topAnchor constraintEqualToAnchor:monthLabel.bottomAnchor constant:5],
        [self.lunarMonthPicker.leadingAnchor constraintEqualToAnchor:monthContainer.leadingAnchor],
        [self.lunarMonthPicker.trailingAnchor constraintEqualToAnchor:monthContainer.trailingAnchor],
        [self.lunarMonthPicker.bottomAnchor constraintEqualToAnchor:monthContainer.bottomAnchor],
        
        // 日期容器
        [dayContainer.topAnchor constraintEqualToAnchor:sectionView.topAnchor constant:50],
        [dayContainer.trailingAnchor constraintEqualToAnchor:sectionView.trailingAnchor constant:-20],
        [dayContainer.widthAnchor constraintEqualToAnchor:sectionView.widthAnchor multiplier:0.45],
        [dayContainer.heightAnchor constraintEqualToConstant:140],
        
        [dayLabel.topAnchor constraintEqualToAnchor:dayContainer.topAnchor constant:10],
        [dayLabel.leadingAnchor constraintEqualToAnchor:dayContainer.leadingAnchor constant:15],
        
        [self.lunarDayPicker.topAnchor constraintEqualToAnchor:dayLabel.bottomAnchor constant:5],
        [self.lunarDayPicker.leadingAnchor constraintEqualToAnchor:dayContainer.leadingAnchor],
        [self.lunarDayPicker.trailingAnchor constraintEqualToAnchor:dayContainer.trailingAnchor],
        [self.lunarDayPicker.bottomAnchor constraintEqualToAnchor:dayContainer.bottomAnchor],
        
        // 出生年
        [yearLabel.topAnchor constraintEqualToAnchor:monthContainer.bottomAnchor constant:20],
        [yearLabel.leadingAnchor constraintEqualToAnchor:sectionView.leadingAnchor constant:20],
        
        [self.yearTextField.topAnchor constraintEqualToAnchor:yearLabel.bottomAnchor constant:10],
        [self.yearTextField.leadingAnchor constraintEqualToAnchor:sectionView.leadingAnchor constant:20],
        [self.yearTextField.trailingAnchor constraintEqualToAnchor:sectionView.trailingAnchor constant:-20],
        [self.yearTextField.heightAnchor constraintEqualToConstant:44],
        [self.yearTextField.bottomAnchor constraintEqualToAnchor:sectionView.bottomAnchor constant:-20]
    ]];
}

- (void)createNotificationSection {
    UIView *sectionView = [self createSectionViewWithTitle:@"提醒设置"];
    
    // 生日当天提醒
    UIView *onDayView = [self createSwitchRowWithTitle:@"生日当天" subtitle:@"00:00 提醒"];
    self.notifyOnDaySwitch = [onDayView viewWithTag:100];
    [sectionView addSubview:onDayView];
    
    // 提前一天提醒
    UIView *oneDayView = [self createSwitchRowWithTitle:@"提前一天" subtitle:@"20:00 提醒"];
    self.notifyOneDayBeforeSwitch = [oneDayView viewWithTag:100];
    [sectionView addSubview:oneDayView];
    
    // 提前一周提醒
    UIView *oneWeekView = [self createSwitchRowWithTitle:@"提前一周" subtitle:@"20:00 提醒"];
    self.notifyOneWeekBeforeSwitch = [oneWeekView viewWithTag:100];
    [sectionView addSubview:oneWeekView];
    
    // 设置约束
    onDayView.translatesAutoresizingMaskIntoConstraints = NO;
    oneDayView.translatesAutoresizingMaskIntoConstraints = NO;
    oneWeekView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [NSLayoutConstraint activateConstraints:@[
        [onDayView.topAnchor constraintEqualToAnchor:sectionView.topAnchor constant:50],
        [onDayView.leadingAnchor constraintEqualToAnchor:sectionView.leadingAnchor constant:20],
        [onDayView.trailingAnchor constraintEqualToAnchor:sectionView.trailingAnchor constant:-20],
        [onDayView.heightAnchor constraintEqualToConstant:60],
        
        [oneDayView.topAnchor constraintEqualToAnchor:onDayView.bottomAnchor constant:10],
        [oneDayView.leadingAnchor constraintEqualToAnchor:sectionView.leadingAnchor constant:20],
        [oneDayView.trailingAnchor constraintEqualToAnchor:sectionView.trailingAnchor constant:-20],
        [oneDayView.heightAnchor constraintEqualToConstant:60],
        
        [oneWeekView.topAnchor constraintEqualToAnchor:oneDayView.bottomAnchor constant:10],
        [oneWeekView.leadingAnchor constraintEqualToAnchor:sectionView.leadingAnchor constant:20],
        [oneWeekView.trailingAnchor constraintEqualToAnchor:sectionView.trailingAnchor constant:-20],
        [oneWeekView.heightAnchor constraintEqualToConstant:60],
        [oneWeekView.bottomAnchor constraintEqualToAnchor:sectionView.bottomAnchor constant:-20]
    ]];
}

- (void)createNoteSection {
    UIView *sectionView = [self createSectionViewWithTitle:@"备注"];
    
    self.noteTextView = [[UITextView alloc] init];
    self.noteTextView.font = [UIFont systemFontOfSize:16];
    self.noteTextView.layer.borderColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0].CGColor;
    self.noteTextView.layer.borderWidth = 1.0;
    self.noteTextView.layer.cornerRadius = 8;
    self.noteTextView.backgroundColor = [UIColor whiteColor];
    self.noteTextView.delegate = self;
    self.noteTextView.text = @"添加备注信息（可选）";
    self.noteTextView.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
    [sectionView addSubview:self.noteTextView];
    
    self.noteTextView.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [self.noteTextView.topAnchor constraintEqualToAnchor:sectionView.topAnchor constant:50],
        [self.noteTextView.leadingAnchor constraintEqualToAnchor:sectionView.leadingAnchor constant:20],
        [self.noteTextView.trailingAnchor constraintEqualToAnchor:sectionView.trailingAnchor constant:-20],
        [self.noteTextView.heightAnchor constraintEqualToConstant:80],
        [self.noteTextView.bottomAnchor constraintEqualToAnchor:sectionView.bottomAnchor constant:-20]
    ]];
}

- (UIView *)createSectionViewWithTitle:(NSString *)title {
    UIView *sectionView = [[UIView alloc] init];
    sectionView.backgroundColor = [UIColor whiteColor];
    sectionView.layer.cornerRadius = 12;
    sectionView.layer.shadowColor = [UIColor blackColor].CGColor;
    sectionView.layer.shadowOffset = CGSizeMake(0, 2);
    sectionView.layer.shadowOpacity = 0.1;
    sectionView.layer.shadowRadius = 8;
    [self.contentView addSubview:sectionView];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    [sectionView addSubview:titleLabel];
    
    titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [titleLabel.topAnchor constraintEqualToAnchor:sectionView.topAnchor constant:20],
        [titleLabel.leadingAnchor constraintEqualToAnchor:sectionView.leadingAnchor constant:20]
    ]];
    
    return sectionView;
}

- (UIView *)createSwitchRowWithTitle:(NSString *)title subtitle:(NSString *)subtitle {
    UIView *rowView = [[UIView alloc] init];
    rowView.backgroundColor = [UIColor whiteColor];
    rowView.layer.cornerRadius = 8;
    rowView.layer.borderWidth = 1.0;
    rowView.layer.borderColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0].CGColor;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    [rowView addSubview:titleLabel];
    
    UILabel *subtitleLabel = [[UILabel alloc] init];
    subtitleLabel.text = subtitle;
    subtitleLabel.font = [UIFont systemFontOfSize:12];
    subtitleLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
    [rowView addSubview:subtitleLabel];
    
    UISwitch *switchControl = [[UISwitch alloc] init];
    switchControl.on = YES;
    switchControl.tag = 100;
    [rowView addSubview:switchControl];
    
    titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    switchControl.translatesAutoresizingMaskIntoConstraints = NO;
    
    [NSLayoutConstraint activateConstraints:@[
        [titleLabel.leadingAnchor constraintEqualToAnchor:rowView.leadingAnchor constant:16],
        [titleLabel.centerYAnchor constraintEqualToAnchor:rowView.centerYAnchor constant:-8],
        
        [subtitleLabel.leadingAnchor constraintEqualToAnchor:rowView.leadingAnchor constant:16],
        [subtitleLabel.centerYAnchor constraintEqualToAnchor:rowView.centerYAnchor constant:8],
        
        [switchControl.trailingAnchor constraintEqualToAnchor:rowView.trailingAnchor constant:-16],
        [switchControl.centerYAnchor constraintEqualToAnchor:rowView.centerYAnchor]
    ]];
    
    return rowView;
}

- (void)setupConstraints {
    self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [NSLayoutConstraint activateConstraints:@[
        [self.scrollView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor],
        [self.scrollView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [self.scrollView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [self.scrollView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
        
        [self.contentView.topAnchor constraintEqualToAnchor:self.scrollView.topAnchor constant:20],
        [self.contentView.leadingAnchor constraintEqualToAnchor:self.scrollView.leadingAnchor constant:20],
        [self.contentView.trailingAnchor constraintEqualToAnchor:self.scrollView.trailingAnchor constant:-20],
        [self.contentView.bottomAnchor constraintEqualToAnchor:self.scrollView.bottomAnchor constant:-20],
        [self.contentView.widthAnchor constraintEqualToAnchor:self.scrollView.widthAnchor constant:-40]
    ]];
    
    // 设置各个section的约束
    NSArray *sectionViews = self.contentView.subviews;
    UIView *previousView = nil;
    
    for (UIView *sectionView in sectionViews) {
        sectionView.translatesAutoresizingMaskIntoConstraints = NO;
        
        if (previousView) {
            [NSLayoutConstraint activateConstraints:@[
                [sectionView.topAnchor constraintEqualToAnchor:previousView.bottomAnchor constant:20]
            ]];
        } else {
            [NSLayoutConstraint activateConstraints:@[
                [sectionView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor]
            ]];
        }
        
        [NSLayoutConstraint activateConstraints:@[
            [sectionView.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor],
            [sectionView.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor]
        ]];
        
        previousView = sectionView;
    }
    
    if (previousView) {
        [NSLayoutConstraint activateConstraints:@[
            [previousView.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:-20]
        ]];
    }
}

- (void)cancelButtonTapped {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)saveButtonTapped {
    if (self.nameTextField.text.length == 0) {
        [self showAlertWithTitle:@"提示" message:@"请输入姓名"];
        return;
    }
    
    if (self.yearTextField.text.length == 0) {
        [self showAlertWithTitle:@"提示" message:@"请输入出生年份"];
        return;
    }
    
    BirthdayModel *birthday = [[BirthdayModel alloc] init];
    birthday.name = self.nameTextField.text;
    birthday.relationship = self.selectedRelationship;
    birthday.lunarMonth = self.selectedLunarMonth;
    birthday.lunarDay = self.selectedLunarDay;
    birthday.birthYear = [self.yearTextField.text integerValue];
    
    // 处理备注信息
    NSString *noteText = self.noteTextView.text;
    if ([noteText isEqualToString:@"添加备注信息（可选）"]) {
        noteText = @"";
    }
    birthday.note = noteText;
    
    birthday.notifyOnDay = self.notifyOnDaySwitch.isOn;
    birthday.notifyOneDayBefore = self.notifyOneDayBeforeSwitch.isOn;
    birthday.notifyOneWeekBefore = self.notifyOneWeekBeforeSwitch.isOn;
    
    if (self.completionBlock) {
        self.completionBlock(birthday);
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)setupKeyboardHandling {
    // 添加键盘显示/隐藏通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    // 添加点击手势来隐藏键盘
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tapGesture];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary *userInfo = notification.userInfo;
    CGRect keyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyboardHeight = keyboardFrame.size.height;
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, keyboardHeight, 0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
}

- (void)keyboardWillHide:(NSNotification *)notification {
    self.scrollView.contentInset = UIEdgeInsetsZero;
    self.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}

- (void)dismissKeyboard {
    [self.view endEditing:YES];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title 
                                                                   message:message 
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" 
                                                       style:UIAlertActionStyleDefault 
                                                     handler:nil];
    [alert addAction:okAction];
    
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - UITextViewDelegate

- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:@"添加备注信息（可选）"]) {
        textView.text = @"";
        textView.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length == 0) {
        textView.text = @"添加备注信息（可选）";
        textView.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
    }
}

#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (pickerView == self.relationshipPicker) {
        return self.relationshipOptions.count;
    } else if (pickerView == self.lunarMonthPicker) {
        return self.lunarMonthOptions.count;
    } else if (pickerView == self.lunarDayPicker) {
        return self.lunarDayOptions.count;
    }
    return 0;
}

#pragma mark - UIPickerViewDelegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (pickerView == self.relationshipPicker) {
        return self.relationshipOptions[row];
    } else if (pickerView == self.lunarMonthPicker) {
        return self.lunarMonthOptions[row];
    } else if (pickerView == self.lunarDayPicker) {
        return self.lunarDayOptions[row];
    }
    return @"";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (pickerView == self.relationshipPicker) {
        self.selectedRelationship = self.relationshipOptions[row];
    } else if (pickerView == self.lunarMonthPicker) {
        self.selectedLunarMonth = row + 1;
    } else if (pickerView == self.lunarDayPicker) {
        self.selectedLunarDay = row + 1;
    }
}

@end 